package com.followme.module.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: Adapter for element list
 * Date: 16.12.13
 * Time: 21:27
 */
public class ElementListAdapter extends ArrayAdapter<ElementList> {

    private List<ElementList> elementLists;
    private Context context;

    public ElementListAdapter(List<ElementList> elementLists, Context ctx) {
        super(ctx, R.layout.row_layout, elementLists);
        this.elementLists = elementLists;
        this.context = ctx;
    }

    public int getCount() {
        return elementLists.size();
    }

    public ElementList getItem(int position) {
        return elementLists.get(position);
    }

    public long getItemId(int position) {
        return elementLists.get(position).hashCode();
    }

    private static class ElementListHolder {
        public TextView nameBigView;
        public TextView nameSmallView;
        public TextView idRoute;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        ElementListHolder holder = new ElementListHolder();

        // First let's verify the convertView is not null
        if (convertView == null) {

            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.row_layout, null);

            // Now we can fill the layout with the right values
            TextView nameB = (TextView) v.findViewById(R.id.name_b);
            TextView nameS = (TextView) v.findViewById(R.id.name_s);
            TextView idRoute = (TextView) v.findViewById(R.id.id_route);

            holder.nameBigView = nameB;
            holder.nameSmallView = nameS;
            holder.idRoute = idRoute;

            v.setTag(holder);
        }else {
            holder = (ElementListHolder) v.getTag();
        }

        ElementList elementList = elementLists.get(position);
        holder.nameBigView.setText(elementList.getNameB());
        holder.nameSmallView.setText(elementList.getNameS());
        holder.idRoute.setText(elementList.getIdRoute());

        return v;
    }
}
